using System;
using System.Collections.Generic;
using System.Text;
using CIM.Model;
using System.Data;

namespace CIM.BLL.Pay
{
    public class ActiveManager:BaseLogic
    {
        public ActiveManager(string connstr):base(connstr)
        {
        }
        public List<double> GetActiveList(string aid)
        {
            List<double> list = new List<double>();
            PayKeyParaManager paramgr = new PayKeyParaManager(db.Connstr);
            PayKeyPara para = paramgr.GetPara(aid, DateTime.MinValue);
            if (para == null)
            {
                return list;
            }
            ArBank bll = new ArBank(db.Connstr);
            ArBankInfo item = bll.GetByKeyField(aid);
            double activeFloat = item.ActiveFloat.HasValue ? item.ActiveFloat.Value : 1;
            for (int i = 0; i < para.LoanTermYear.Value; i++)
            {
                double? active = GetStandardActive(para.Getloandate.Value.AddYears(i), para.LoanTermYear.Value);
                if (active.HasValue)
                {
                    list.Add(active.Value*activeFloat);
                }
            }
            return list;
        }



        public double? GetStandardActive(DateTime when, int loanTermYear)
        {
            DataTable dt = dt = db.Select("select top 1 active from mwactive where loanterm=" + loanTermYear + " and datediff(\"d\",executeDate,'" + when.ToShortDateString() + "')>=0 order by executeDate desc");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            double? standActive = double.Parse(dt.Rows[0]["active"].ToString());
            return standActive;
        }
    }
}
